#include <memory>
#include "streamPushWrapper.h"
#include "LiveService.h"
#include "ZYUtils/ZYLog.h"

static std::string gVersion = "1.00";
static std::string gUrl ;

static std::shared_ptr<ZYLive::LiveService> gLiveService = nullptr;
bool createPushService(){
    if(gLiveService ){
        gLiveService.reset();
        gLiveService = nullptr;
    }

    gLiveService = std::make_shared<ZYLive::LiveService>();
    // gLiveService->setPushInfo("rtmp");
    gLiveService->setPushInfo("rtsp");
    return true;
}

const uint8_t * getPushServiceVersion()
{
    return  reinterpret_cast<const uint8_t*>(gVersion.c_str());
}

void releasePushService(){
    if(gLiveService){
        gLiveService.reset();
        gLiveService = nullptr;
    }
}

const uint8_t *getUrlFromPushService()
{
    if(gLiveService){
        gUrl = gLiveService->getUrl();
        return reinterpret_cast<const uint8_t*>(gUrl.c_str());
    }
    return nullptr;

}

bool setVideoInfoForPushService(int width,int height,int fps,float bitrate)
{
    if(gLiveService){
        auto bitSize = bitrate * 1024*1024;
        return gLiveService->setVideoInfo(width,height,fps,bitSize);
    }
    return false;
}

bool pushVideoDataForPushServiceWithYUVData(uint8_t* pData,int width,int height)
{
    if(gLiveService){
        auto ret =  gLiveService->pushVideoData(pData, 0,width,height);
        if(ret == 0){
            return true;
        }
    }

    return false;
}

bool pushVideoDataForPushServiceWithBGRAData(uint8_t* pData,int width,int height)
{
    if(gLiveService){
        auto ret =  gLiveService->pushVideoData(pData, 2,width,height);
        if(ret == 0){
            return true;
        }
    }

    return false;
}
bool pushVideoDataForPushServiceWithRGBAData(uint8_t *pData,int width,int height)
{
    if(gLiveService){
        auto ret =  gLiveService->pushVideoData(pData, 1,width,height);
        if(ret == 0){
            return true;
        }
    }

    return false;

//    if(gLiveService) {
//        cv::Mat rgba(height, width, CV_8UC4, pData);
//        cv::Mat yuv(height * 3 / 2, width, CV_8UC1);
//        cv::cvtColor(rgba, yuv, cv::COLOR_RGBA2YUV_I420);
//
//        auto ret = gLiveService->pushVideoData(yuv.data,0);
//        if(ret == 0 ){
//            return true;
//        }
//    }
//
//    return false;


}

